package gui;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import application.Aesp;

/**
 * Builds a new JFrame window with a table. If a project file has been
 * imported, the data in it will be displayed in the table. The table will be empty 
 * as default.
 */
public class ProjectEffortsTable extends JFrame {
	private Object[][] data;
	private boolean hasData;

	public ProjectEffortsTable(Aesp aesp, boolean hasData) {
		super("Imported Projects");
		this.hasData = hasData;
		build(aesp);
	}

	/**
	 * Sets up the content in the frame
	 */
	private void build(Aesp aesp) {
		setLayout(new BorderLayout());
		String[] columnNames = new String[aesp.getCharsNames().length+1];
		columnNames[0] = "Project";
		int count = 0;
		for (int i = 1; i < columnNames.length; i++) {
			String[] s = aesp.getCharsNames();
			columnNames[i] = s[count];
			count++;
		}
		data = new Object[100][columnNames.length];
		JTable table = new JTable(data, columnNames);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setFillsViewportHeight(true);
		table.getTableHeader().setReorderingAllowed(false);
		table.setRowSelectionAllowed(false);
		table.setColumnSelectionAllowed(false);
		table.setEnabled(false);
		JScrollPane scroll = new JScrollPane(table);
		add(scroll, BorderLayout.CENTER);
		
		if(hasData) {
			addData(aesp);
		}
		
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		table.setDragEnabled(false);
		
		pack();
		setVisible(true);
	}

	/**
	 * Adds the imported file data to the table
	 */
	private void addData(Aesp aesp) {
		for (int i = 0; i < aesp.size(); i++) {
			for (int j = 0; j < data[i].length; j++) {
				if (j == 0) {
					String name = "Proj";
					int nbr = i+1;
					if (nbr < 10) {
						name = name + "00" + nbr;
					} else if (nbr < 100) {
						name = name + "0" + nbr;
					} else {
						name = name + nbr;
					}
					data[i][j] = name;
				} else{
					String rawData[][] = aesp.getInputData();
					int col = j-1;
					data[i][j] = rawData[i][col];
				}
			}
		}
	}
}
